package com.shiwaixiangcun.monitor.controller;

import com.shiwaixiangcun.core.plugin.dto.MonkeyDto;
import com.shiwaixiangcun.core.plugin.web.validator.ConstraintType;
import com.shiwaixiangcun.core.plugin.web.validator.ParameterConstraint;
import com.shiwaixiangcun.core.plugin.web.validator.ParameterConstraints;
import com.shiwaixiangcun.monitor.domain.User;
import com.shiwaixiangcun.monitor.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.constraints.NotNull;

@Controller
@RequestMapping("/staff")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 新增员工
     */
    @ParameterConstraints({
            @ParameterConstraint(name = "name", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "姓名不能为空")),
            @ParameterConstraint(name = "password", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "密码不能为空")),
            @ParameterConstraint(name = "phone", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "电话不能为空")),
            @ParameterConstraint(name = "number", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "工号不能为空"))
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addUser(MonkeyDto monkeyDto) {
        userService.addUser(monkeyDto);
        return "SUCCESS:添加成功";
    }

    /**
     * 编辑员工
     */
    @ParameterConstraints({
            @ParameterConstraint(name = "staffId", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "员工id不能为空"))
    })
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public String updateUser(MonkeyDto monkeyDto) {
        userService.updateUser(monkeyDto);
        return "SUCCESS:编辑成功";
    }

    /**
     * 查询员工列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Page<User> getStaffList(Pageable pageable, String search) {
        return userService.getStaffList(pageable, search);
    }

    /**
     * 员工对象选择
     * */
    @RequestMapping(value = "/listData", method = RequestMethod.GET)
    public Page<User> getStaffObjectList(Pageable pageable, String search) {
        return userService.getStaffObjectList(pageable, search);
    }

    /**
     * 更新员工状态
     */
    @RequestMapping(value = "/isEnable", method = RequestMethod.PUT)
    public String updateStatus(Long staffId) {
        userService.updateStatus(staffId);
        return "SUCCESS:更新成功";
    }


    @RequestMapping(value = "/password/modify", method = RequestMethod.PUT)
    public String modifyPassword(@RequestParam String oldPassword, @RequestParam String newPassword) {
        userService.passwordModify(oldPassword, newPassword);
        return "SUCCESS:修改成功";
    }



}
